<?php
header("content-type:text/html; charset=utf-8");
session_start();
require_once 'inc/config.php';
require_once 'inc/classes/db.php';
require_once 'inc/classes/template.php';
require_once 'inc/classes/util.php';
require_once 'include/staticdata.php';

$gTemplate['templateDir'] = 'templates/';
$gTemplate['leftDelimiter'] = '{';
$gTemplate['rightDelimiter'] = '}';
$tpl = new Template( $gTemplate );
$db  = new db( $gDb );
util::gpc( );

if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
	$username = addslashes(trim($_POST['username']));
	$pwd      = trim($_POST['pwd']);
	$confirm  = trim($_POST['confirmpwd']);
	$email    = trim($_POST['email']);
	$user = $db->fetchrow("select * from ".$gDb['prefix']."users where username='$username'");
	if( empty($user) ) {
		$now = time();
		$regip = $_SERVER['REMOTE_ADDR'];
		$pwd = md5($pwd);
		$db->query("insert into ".$gDb['prefix']."users (username, pwd, email, regtime, regip)values('$username','$pwd','$email','$now','$regip')");
		header('location: login.php');
		exit();
	}
	$data['username'] 	= $username;
	$data['pwd'] 		= $pwd;
	$data['email'] 		= $email;
	$data['confirm'] 	= $confirm;
	$tpl->assign('data', $data);
}

$tpl->assign('topnav', 	StaticData::getMenuData());
$tpl->assign('smallnav',StaticData::getSmallNav());
$tpl->assign('sort', 	StaticData::getSort());
$tpl->assign('lang',	include('include/lang.php'));

$tpl->display( );